/**
 * 定义页面对应的后台处理类完整路径。
 */
var clsUrl = basePath + 'JarpListener?className=cn.com.zsc.hr.AttendanceManager';

Ext.onReady(function() {
			Ext.QuickTips.init();
			
	/**
	 * 日期时间控件
	 */
	var searchDate = new Ext.form.DateField({
		id:'searchDate',
        fieldLabel:"考勤时间",
        name:'searchDate',
        format:'Y-m-d',
        value:new Date(),
        editable:false,
        width:90
    });
    
    /**
	 * 考勤状态数据源
	 */
	var statusStore=new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : comboBoxUrl + '51'
		}),
		reader : new Ext.data.JsonReader({
			fields : [
			{name:'CL_CODE'},
			{name:'CL_NAME'}
		]
		}),
		autoLoad : true
	});
	
	/**
	 * 考勤状态控件
	 */
	var statusComboBox = new Ext.form.ComboBox({
		fieldLabel : '考勤状态',
		id : 'status',
		width : 90,
		displayField : 'CL_NAME',
		valueField : 'CL_CODE',
		emptyText : '请选择',
		store:statusStore,
		mode : 'local',
		editable  : false,
		triggerAction : 'all',
		selectOnFocus : true
	});
    
    var storereader= new Ext.data.JsonReader({
    	fields:[
    		{name :'WORKID'},
    		{name :'SHOW_ENAME'},
    		{name :'RECDATE'},
    		{name :'RECTIME'},
    		{name : 'SHOW_DUTIES'},
    		{name : 'SHOW_TERM'},
    		{name :'SHOW_STATUS'}
    	]
	});
		
	/**
	 * 查询员工考勤数据
	 */
	var store = new Ext.data.Store({
		reader:storereader,
		proxy:new Ext.data.HttpProxy({
			url:clsUrl+'&methodName=getDayInfo&&paramCount=0'
		})
	});
	
	var searchPanel = new Ext.Panel({
		id:"searchPanel",
		region:"north",
		border:false,
		layout : 'column',
		labelAlign : 'right',
		height:40,
		frame:true,
		labelWidth : 80,
		items:[{
			layout : 'form',
			border:false,
		  	labelWidth : 40,
		  	labelAlign : 'right',
		  	columnWidth : .20,
		  	items:[
		  		   {fieldLabel : '姓名',width : 100,name : 'emplName',id : 'emplName',xtype : 'textfield'}
		  		  ]
		},
			{
			layout : 'form',
			border:false,
		  	labelWidth : 40,
		  	labelAlign : 'right',
		  	columnWidth : .20,
		  	items:[
		  		   {fieldLabel : '工号',width : 100,name : 'workID',id : 'workID',xtype : 'textfield'}
		  		  ]
		},{
			layout : 'form',
		  	labelWidth : 80,
		  	labelAlign : 'right',
		  	columnWidth : .30,
		  	items:[searchDate]
		},{
			layout : 'form',
		  	labelWidth : 0,
		  	labelAlign : 'right',
		  	columnWidth : .30,
		  	items:[{
	  			layout : 'column',
				labelAlign : 'right',
				items:[{
					layout : 'form',
				  	labelWidth : 10,
				  	labelAlign : 'right',
				  	columnWidth : .33,
				  	items:[
				  		{xtype:'button',width:70,text:'查询',handler:search},
				  		{height:4}
				  	]
				},{
					layout : 'form',
				  	labelWidth : 10,
				  	labelAlign : 'right',
				  	columnWidth : .33,
				  	items:[
				  		{xtype:'button',width:70,text:'重置',handler:function() {
				  				Ext.getCmp("emplName").setValue("");
					  			Ext.getCmp("workID").setValue("");
					  			Ext.getCmp("searchDate").setValue(new Date());
							}
						}
				    ]
				},{
					layout : 'form',
				  	labelWidth : 10,
				  	labelAlign : 'right',
				  	columnWidth : .34,
				  	items:[
				  		{xtype:'button',width:70,text:'导出Excel',handler:exportExcel}
				  	]
				}]
			}]
		}],
		keys: {
            key: 13,
            fn: search
        }
	});
	
	
	var basegrid = new Ext.grid.EditorGridPanel( {
		id :'basegrid',
		store :store,
		split :true,
		bodyStyle:'5px 15px 0',
		region :'center',
		border :false,
		enableColumnMove:false,   
		enableHdMenu:false,
		frame :false,
		autoScroll :true,
		viewConfig: {
        	forceFit: true
    	},
    	columns: [
     	            new Ext.grid.RowNumberer(),
    	          	{header:'工号',sortable:true,enableColumnMove:false,enableColumnResize :false, dataIndex: 'WORKID'},
    	          	{header:'姓名',sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'SHOW_ENAME'},
    	          	{header:'岗位',sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'SHOW_DUTIES'},
    	          	{header:'职工组别',sortable:true,hidden:false,enableColumnMove:false,enableColumnResize :false, dataIndex: 'SHOW_TERM'},
    	          	{header:'考勤日期',sortable:true,enableColumnMove:false,enableColumnResize :false, dataIndex: 'RECDATE'},
    	          	{header:'考勤时间',sortable:true,enableColumnMove:false,enableColumnResize :false, dataIndex: 'RECTIME',editor:new Ext.form.TextField()},
    	          	{header:'考勤状态',sortable:true,enableColumnMove:false,enableColumnResize :false, dataIndex: 'SHOW_STATUS',editor:statusComboBox}
    	],
    	tbar:[     
      	   	{
	      	   	text:'保存',
	      	   	iconCls:'add',
	      	   	handler:function(){
      	   			update();
      	   		}
      	   	}
 	    ],
    	listeners:{'afteredit':function(obj) {
    			var record = obj.record;
    			var editedValue = record.get(obj.field);
    			
    			if(editedValue==229) {
    				record.set(obj.field, "公假");
    			} else if(editedValue==230) {
    				record.set(obj.field, "出勤");
    			} else if(editedValue==231) {
    				record.set(obj.field, "迟到");
    			} else if(editedValue==232) {
    				record.set(obj.field, "病假");
    			} else if(editedValue==237) {
    				record.set(obj.field, "工伤");
    			} else if(editedValue==238) {
    				record.set(obj.field, "丧假");
    			} else if(editedValue==239) {
    				record.set(obj.field, "生育");
    			} else if(editedValue==240) {
    				record.set(obj.field, "事假");
    			} else if(editedValue==242) {
    				record.set(obj.field, "旷工");
    			} else if(editedValue==243) {
    				record.set(obj.field, "探亲");
    			}
    		}
    	}
	});
	
	store.load();
	var mainPanel = new Ext.Panel({
            id: 'mainPanel',
            region:'center',
            split:true,
            border:false,
            frame:true,
            layout: 'border',  
            items:[searchPanel,basegrid]
    });
    
	var viewport = new Ext.Viewport( {
		layout:'border',
		items: [mainPanel]
	});
	
	function update()
	{
		var modifyRecord = store.getModifiedRecords();
		
		if (modifyRecord.length == 0) {
			Ext.Msg.show({
				title : '提示',
				width : 210,
				msg : '请先修改数据!',
				buttons : Ext.Msg.OK,
				icon : Ext.Msg.ERROR
			});
			return;
		}

		mainPanel.body.mask('正在保存，请稍等', 'x-mask-loading');
		var obj;
		var recordModStr = "[";
		
		for (var i=0; i<modifyRecord.length; i++) {
			obj = new Object();
			
			if(modifyRecord[i].data.SHOW_STATUS=="公假") {
    			obj.status = "229";
    		} else if(modifyRecord[i].data.SHOW_STATUS=="出勤") {
    			obj.status = "230";
    		} else if(modifyRecord[i].data.SHOW_STATUS=="迟到") {
    			obj.status = "231";
    		} else if(modifyRecord[i].data.SHOW_STATUS=="病假") {
    			obj.status = "232";
    		} else if(modifyRecord[i].data.SHOW_STATUS=="工伤") {
    			obj.status = "237";
			} else if(modifyRecord[i].data.SHOW_STATUS=="丧假") {
				obj.status = "238";
			} else if(modifyRecord[i].data.SHOW_STATUS=="生育") {
				obj.status = "239";
			} else if(modifyRecord[i].data.SHOW_STATUS=="事假") {
				obj.status = "240";
			} else if(modifyRecord[i].data.SHOW_STATUS=="旷工") {
				obj.status = "242";
			} else if(modifyRecord[i].data.SHOW_STATUS=="探亲") {
				obj.status = "243";
			}
    		
    		obj.workID = modifyRecord[i].data.WORKID;
    		obj.recDate = modifyRecord[i].data.RECDATE;
    		obj.recTime = modifyRecord[i].data.RECTIME;
			
			recordModStr += Ext.util.JSON.encode(obj);
			
			if(i < modifyRecord.length - 1) {
				recordModStr += ",";   
			}
		}
		
		recordModStr += "]";
		
		var url=clsUrl+'&methodName=updateAttendance&paramCount=0'
		
    	httpReq=null;
		httpReq = getXMLHttpRequest();
    	httpReq.onreadystatechange = onUpdateRtn;
		httpReq.open("POST",url, true);
		httpReq.send(recordModStr);		
	}
	
	function onUpdateRtn(){
		if(httpReq.readyState == 4){
		   	if(httpReq.status == 200){
		   		if(httpReq.responseText == "success"){
		   			Ext.Msg.show({   
		    	   		title : '成功提示',
		    	   		width:200,
		   				msg : '保存成功！',  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.INFO   
		   			});
					
		   			store.commitChanges();
		   			search();
		   			mainPanel.body.unmask();
		   			
		   			httpReq=null;
		   		}else{
		   			Ext.Msg.show({   
		    	   		title : '错误提示',
		    	   		width:200,
		   				msg : httpReq.responseText,  
			       		buttons : Ext.Msg.OK,   
			       		icon : Ext.Msg.ERROR   
		   			});  
		   			httpReq=null;
		   		}
		   	}
		}
	}
	
    function search(){
		mainPanel.body.mask('正在加载数据...','x-mask-loading');
		var obj = new Object();
		obj.emplName = Ext.getCmp("emplName").getValue();
		obj.workID = Ext.getCmp("workID").getValue();
		obj.recDate = Ext.getCmp("searchDate").getValue();
		
    	var url=clsUrl+'&methodName=getDayInfo&paramCount=0'
		Ext.http.send(url,function(res){
			basegrid.store.loadData(Ext.decode(res));
			mainPanel.body.unmask();
		},Ext.encode(obj));
	}
	
	function exportExcel() {
		var vExportContent = basegrid.getExcelXml(); // 获取数据
		if (Ext.isIE6 || Ext.isIE7 || Ext.isIE8 || Ext.isSafari || Ext.isSafari2 || Ext.isSafari3) { // 判断浏览器
			var fd = Ext.get('frmDummy');
			if (!fd) {
				fd = Ext.DomHelper.append(Ext.getBody(), {
							tag : 'form',
							method : 'post',
							id : 'frmDummy',
							action : exportUrl,
							target : '_blank',
							name : 'frmDummy',
							cls : 'x-hidden',
							cn : [{
										tag : 'input',
										name : 'exportContent',
										id : 'exportContent',
										type : 'hidden'
									}]
						}, true);
			}
			fd.child('#exportContent').set({
						value : vExportContent
					});
			fd.dom.submit();
		} else {
			document.location = 'data:application/vnd.ms-excel;base64,'+ Base64.encode(vExportContent);
		}
	}
	
	store.on('beforeload',function(thiz,options){
		mainPanel.body.mask('正在加载数据','x-mask-loading');
    });
    
	store.on("load",function(){
		mainPanel.body.unmask();
	});
	
});